#pragma once
#include "txc_dxtn.h"

#pragma pack(push,packing)
#pragma pack(1)
typedef struct _DDCOLORKEYx
{
	unsigned long dwColorSpaceLowValue;
	unsigned long dwColorSpaceHighValue;
} DDCOLORKEYx;

typedef struct _DDPIXELFORMATx
{
	unsigned long       dwSize;                 // size of structure
	unsigned long       dwFlags;                // pixel format flags
	unsigned long       dwFourCC;               // (FOURCC code)
	union
	{
		unsigned long   dwRGBBitCount;          // how many bits per pixel
		unsigned long   dwYUVBitCount;          // how many bits per pixel
		unsigned long   dwZBufferBitDepth;      // how many total bits/pixel in z buffer (including any stencil bits)
		unsigned long   dwAlphaBitDepth;        // how many bits for alpha channels
		unsigned long   dwLuminanceBitCount;    // how many bits per pixel
		unsigned long   dwBumpBitCount;         // how many bits per "buxel", total
		unsigned long   dwPrivateFormatBitCount;// Bits per pixel of private driver formats. Only valid in texture
	};
	union
	{
		unsigned long   dwRBitMask;             // mask for red bit
		unsigned long   dwYBitMask;             // mask for Y bits
		unsigned long   dwStencilBitDepth;      // how many stencil bits (note: dwZBufferBitDepth-dwStencilBitDepth is total Z-only bits)
		unsigned long   dwLuminanceBitMask;     // mask for luminance bits
		unsigned long   dwBumpDuBitMask;        // mask for bump map U delta bits
		unsigned long   dwOperations;           // DDPF_D3DFORMAT Operations
	};
	union
	{
		unsigned long   dwGBitMask;             // mask for green bits
		unsigned long   dwUBitMask;             // mask for U bits
		unsigned long   dwZBitMask;             // mask for Z bits
		unsigned long   dwBumpDvBitMask;        // mask for bump map V delta bits
		struct
		{
			unsigned short    wFlipMSTypes;       // Multisample methods supported via flip for this D3DFORMAT
			unsigned short    wBltMSTypes;        // Multisample methods supported via blt for this D3DFORMAT
		} MultiSampleCaps;

	};
	union
	{
		unsigned long   dwBBitMask;             // mask for blue bits
		unsigned long   dwVBitMask;             // mask for V bits
		unsigned long   dwStencilBitMask;       // mask for stencil bits
		unsigned long   dwBumpLuminanceBitMask; // mask for luminance in bump map
	};
	union
	{
		unsigned long   dwRGBAlphaBitMask;      // mask for alpha channel
		unsigned long   dwYUVAlphaBitMask;      // mask for alpha channel
		unsigned long   dwLuminanceAlphaBitMask;// mask for alpha channel
		unsigned long   dwRGBZBitMask;          // mask for Z channel
		unsigned long   dwYUVZBitMask;          // mask for Z channel
	};
} DDPIXELFORMATx;

typedef struct _DDSCAPS2x
{
	unsigned long       dwCaps;         // capabilities of surface wanted
	unsigned long       dwCaps2;
	unsigned long       dwCaps3;
	union
	{
		unsigned long       dwCaps4;
		unsigned long       dwVolumeDepth;
	};
} DDSCAPS2x;

typedef struct _DDSURFACEDESC2x
{
	unsigned long               dwSize;                 // size of the DDSURFACEDESC structure
	unsigned long               dwFlags;                // determines what fields are valid
	unsigned long               dwHeight;               // height of surface to be created
	unsigned long               dwWidth;                // width of input surface
	union
	{
		long            lPitch;                 // distance to start of next line (return value only)
		unsigned long           dwLinearSize;           // Formless late-allocated optimized surface size
	};
	union
	{
		unsigned long           dwBackBufferCount;      // number of back buffers requested
		unsigned long           dwDepth;                // the depth if this is a volume texture 
	};
	union
	{
		unsigned long           dwMipMapCount;          // number of mip-map levels requestde
		unsigned long           dwRefreshRate;          // refresh rate (used when display mode is described)
		unsigned long           dwSrcVBHandle;          // The source used in VB::Optimize
	};
	unsigned long               dwAlphaBitDepth;        // depth of alpha buffer requested
	unsigned long               dwReserved;             // reserved
	void*              lpSurface;              // pointer to the associated surface memory
	union
	{
		_DDCOLORKEYx      ddckCKDestOverlay;      // color key for destination overlay use
		unsigned long           dwEmptyFaceColor;       // Physical color for empty cubemap faces
	};
	_DDCOLORKEYx          ddckCKDestBlt;          // color key for destination blt use
	_DDCOLORKEYx          ddckCKSrcOverlay;       // color key for source overlay use
	_DDCOLORKEYx          ddckCKSrcBlt;           // color key for source blt use
	union
	{
		_DDPIXELFORMATx   ddpfPixelFormat;        // pixel format description of the surface
		unsigned long           dwFVF;                  // vertex format description of vertex buffers
	};
	_DDSCAPS2x            ddsCaps;                // direct draw surface capabilities
	unsigned long        dwTextureStage;         // stage in multitexture cascade
} DDSURFACEDESC2x;

#pragma pack(pop,packing)
#ifndef MAKEFOURCC
#define MAKEFOURCC(ch0, ch1, ch2, ch3)                              \
	((unsigned long)(unsigned char)(ch0) | ((unsigned long)(unsigned char)(ch1) << 8) |   \
	((unsigned long)(unsigned char)(ch2) << 16) | ((unsigned long)(unsigned char)(ch3) << 24 ))
#endif
#define FOURCC_DXT1  (MAKEFOURCC('D','X','T','1'))
#define FOURCC_DXT2  (MAKEFOURCC('D','X','T','2'))
#define FOURCC_DXT3  (MAKEFOURCC('D','X','T','3'))
#define FOURCC_DXT4  (MAKEFOURCC('D','X','T','4'))
#define FOURCC_DXT5  (MAKEFOURCC('D','X','T','5'))

struct dds_image_info
{
	dds_image_info()
	{
		dwWidth = 0;
		dwHeight = 0;
		dwColorNum = 0;
		dwByteNum = 0;
	}
	unsigned int dwWidth;
	unsigned int dwHeight;
	unsigned int dwColorNum;
	unsigned int dwByteNum;
	unsigned int dwType;
	unsigned int dwAlphaBitDepth;
	unsigned int dwMipFactor;
	unsigned int dwGBitMask;
	unsigned int dwRBitMask;
	unsigned int dwRGBAlphaBitMask;
};






void set_dds_data(unsigned char * pData);
void read_dds_surface_info(DDSURFACEDESC2x * pSurfaceDesc);
void read_dds_image(unsigned char ** ppOut, dds_image_info * pInfo);
